package com.javainterface.service;

//import com.javainterface.mapper.UserMapper;

import com.javainterface.entity.UserLogin;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 将Xml字符串解析成Json字符串
 */
@Service
public class XmlRead {

//    @Autowired
//    UserMapper userMapper;

    //取待办已办我的待阅已阅等文件列表,getList.getDealList()
    public JSONObject xmlToJson_getDealList(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_dealedWorkFlow = (Element) it_data.next();
            Iterator it_dealedWorkFlow = element_dealedWorkFlow.elementIterator("dealedWorkFlow");
            while (it_dealedWorkFlow.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_dealedWorkFlow.next();
                map_child.put("workTitle", element_3.elementTextTrim("workTitle"));
                map_child.put("workSubmitTime", element_3.elementTextTrim("workSubmitTime"));
                map_child.put("jumpLink", "http://10.0.12.181:8070" + element_3.elementTextTrim("jumpLink"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    //解析邮件列表
    public JSONObject xmlToJson_getMailReceiveBoxList(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_infoList = (Element) it_data.next();
            Iterator it_infoList = element_infoList.elementIterator("list");
            while (it_infoList.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_infoList.next();
                map_child.put("mailsubject", element_3.elementTextTrim("mailsubject"));
                map_child.put("mailposttime", element_3.elementTextTrim("mailposttime"));
                map_child.put("jumpLink", "http://10.0.12.181:8070" + element_3.elementTextTrim("jumpLink"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    //取信息列表,information_getMobileList()
    public JSONObject xmlToJson_getMobileList(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_infoList = (Element) it_data.next();
            Iterator it_infoList = element_infoList.elementIterator("infoList");
            while (it_infoList.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_infoList.next();
                map_child.put("informationTitle", element_3.elementTextTrim("informationTitle"));
                map_child.put("informationIssuer", element_3.elementTextTrim("informationIssuer"));
                map_child.put("informationIssueTime", element_3.elementTextTrim("informationIssueTime"));
                map_child.put("channelName", element_3.elementTextTrim("channelName"));
                map_child.put("informationIssueOrg", element_3.elementTextTrim("informationIssueOrg"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    //解析新闻栏目列表
    public JSONObject xmlToJson_getAllInfoChannels(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_infoList = (Element) it_data.next();
            Iterator it_infoList = element_infoList.elementIterator("informationChannel");
            while (it_infoList.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_infoList.next();
                //栏目名称
                map_child.put("channelName", element_3.elementTextTrim("channelName"));
                //栏目类型
                map_child.put("channelType", element_3.elementTextTrim("channelType"));
                //新闻ID
                map_child.put("channelId", element_3.elementTextTrim("channelId"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    public JSONObject xmlToJson_getInformationList(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_infoList = (Element) it_data.next();
            Iterator it_infoList = element_infoList.elementIterator("infoList");
            while (it_infoList.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_infoList.next();
                //信息标题
                map_child.put("informationTitle", element_3.elementTextTrim("informationTitle"));
                //发布日期
                map_child.put("informationModifyTime", element_3.elementTextTrim("informationModifyTime"));
                //url
                map_child.put("jumpLink", "http://10.0.12.181:8070" + element_3.elementTextTrim("jumpLink"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    //解析会议列表
    public JSONObject mxlToJson_getMeetingNoticeList(String xml) {
        Document doc = null;
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map> list = new ArrayList<>();
        map.put("code", 200);
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_infoList = (Element) it_data.next();
            Iterator it_infoList = element_infoList.elementIterator("noticeList");
            while (it_infoList.hasNext()) {
                Map<String, Object> map_child = new HashMap<String, Object>();
                Element element_3 = (Element) it_infoList.next();
                //会议主题
                map_child.put("meetingMotif", element_3.elementTextTrim("meetingMotif"));
                //会议日期
                map_child.put("meetingDate", element_3.elementTextTrim("meetingDate").substring(0, 10));
                //会议url
                map_child.put("jumpLink", "http://10.0.12.181:8070" + element_3.elementTextTrim("jumpLink"));
                list.add(map_child);
            }
        }
        map.put("data", list);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return jsonObject;
    }

    public UserLogin xmlToObject_getUserInfoByUserAccounts(String xml, String userAccount) {
        UserLogin userLogin = new UserLogin();
        Document doc = null;
        try {
            doc = (Document) DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //指向根节点
        Element root = doc.getRootElement();
        //获取子节点data
        Iterator it_data = root.elementIterator("data");
        // 遍历子节点data
        while (it_data.hasNext()) {
            Element element_data = (Element) it_data.next();
            userLogin.setUserId(element_data.elementTextTrim("id"));
            userLogin.setUserAccount(userAccount);
            userLogin.setOrgid(element_data.elementTextTrim("orgid"));
            userLogin.setOrgidstring(element_data.elementTextTrim("orgidstring"));
        }
        return userLogin;
    }

//    获取用户信息列表，getUserList()
//    public void userInsert_getUserList(String xml) {
//        Document doc = null;
//        try {
//            doc = (Document) DocumentHelper.parseText(xml);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        }
//        //指向根节点
//        Element root = doc.getRootElement();
//        //获取子节点data
//        Iterator it_data = root.elementIterator("data");
//        // 遍历子节点data
//        while (it_data.hasNext()) {
//            Element element_list = (Element) it_data.next();
//            Iterator it_list = element_list.elementIterator("list");
//            int cont = 1;
//            while (it_list.hasNext()) {
//                Element element_3 = (Element) it_list.next();
//                userMapper.userInsert(
//                        element_3.elementTextTrim("name"),
//                        element_3.elementTextTrim("orgid"),
//                        element_3.elementTextTrim("orgname"),
//                        element_3.elementTextTrim("sex"),
//                        element_3.elementTextTrim("email"),
//                        element_3.elementTextTrim("userisactive"),
//                        element_3.elementTextTrim("useraccounts"),
//                        element_3.elementTextTrim("orgserial"),
//                        element_3.elementTextTrim("orgidstring"),
//                        element_3.elementTextTrim("orgnamestring"),
//                        element_3.elementTextTrim("userId")
//                );
//                System.out.println("cout:" + cont + ",插入数据：" + element_3.elementTextTrim("name"));
//                cont += 1;
//            }
//        }
//    }

}
